from rest_framework import serializers
from .models import Activity, ActivityParticipant, ActivityTag

class ActivitySerializer(serializers.ModelSerializer):
    participants_count = serializers.SerializerMethodField()
    tags = serializers.SerializerMethodField()
    joined = serializers.SerializerMethodField()
    
    class Meta:
        model = Activity
        fields = '__all__'
    
    def get_participants_count(self, obj):
        return obj.participants.count()
    
    def get_tags(self, obj):
        return [tag.name for tag in obj.tags.all()]
    
    def get_joined(self, obj):
        request = self.context.get('request')
        if not request or not request.GET.get('user_id'):
            return False
        return obj.participants.filter(user_id=request.GET.get('user_id')).exists()


class ActivityParticipantSerializer(serializers.ModelSerializer):
    user_name = serializers.CharField(source='user.username')

    class Meta:
        model = ActivityParticipant
        fields = ['user_name', 'joined_at']